package com.snail.snailvr.views.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.snail.snailvr.R;
import com.snail.snailvr.model.NavDrawerItem;
import com.snail.snailvr.utils.Constant;
import com.snail.snailvr.utils.LockUtil;
import com.snail.snailvr.utils.Util;
import com.snail.snailvr.views.GestureSetActivity;
import com.snail.snailvr.views.GestureUnlockActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 导航栏Adapter
 * Created by snail on 16/4/19.
 */
public class NavigationAdapter extends BaseAdapter {
    public static final int NAV_COUNT=4;
    public static final int NAV_UPGRADE=0,NAV_WIFI_ONLY=1,NAV_PATTERN=1,NAV_FEEDBACK=3;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<NavDrawerItem>mNavDrawerItems;
    public NavigationAdapter(Context context,List<NavDrawerItem>navDrawerItems){
        mContext=context;
        mLayoutInflater=LayoutInflater.from(context);
        mNavDrawerItems=navDrawerItems;
    }
    @Override
    public int getCount() {
        return mNavDrawerItems.size();
    }

    @Override
    public NavDrawerItem getItem(int position) {
        if(position<getCount())
            return mNavDrawerItems.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView=mLayoutInflater.inflate(R.layout.nav_item_layout,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }
        NavDrawerItem item=getItem(position);

        if(item!=null){
            holder.itemView.setOnClickListener(item.getOnClickListener());
            holder.mNavIcon.setImageResource(item.getIconResId());
            holder.mNavTitle.setText(item.getTitle());

            if( item.isShowToggle() ) {
                /*if(position == NAV_WIFI_ONLY) {
                    holder.mToggleButton.setVisibility(View.VISIBLE);
                    holder.mToggleButton.setChecked(SharedPreferencesUtil.getInstance(mContext).isWifiOnly());
                    holder.mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            buttonView.setChecked(isChecked);
                            SharedPreferencesUtil.getInstance(mContext).isWifiOnly(isChecked);
                        }
                    });
                }
                else */if(position == NAV_PATTERN){
                    holder.mToggleButton.setVisibility(View.VISIBLE);

                    int[] mIndexs = LockUtil.getPwd(mContext);
                    // 判断当前是否设置了密码
                    final boolean hasPsw = mIndexs.length>=Constant.PATTERN_PSW_MIN_LENGTH?true:false;
                    holder.mToggleButton.setChecked(hasPsw);
                    holder.mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                            if (!hasPsw && isChecked ){
                                Intent intent = GestureSetActivity.getIntent((Activity)mContext);
                                ((Activity) mContext).startActivityForResult(intent, Constant.GESTURE_CODE);
                            }
                            else if(hasPsw && !isChecked){
                                Intent intent = GestureUnlockActivity.getIntent((Activity)mContext, true);
                                ((Activity) mContext).startActivityForResult(intent, Constant.GESTURE_CODE);
                            }
                        }
                    });
                }
            }
            else{
                holder.mToggleButton.setVisibility(View.GONE);
            }

            if(item.getSub_title()!=null) {
                holder.mNavSubTitle.setVisibility(View.VISIBLE);

                if(item.isShowDrawable()) {
                    Util.setDrawableLeft(holder.mNavSubTitle,R.drawable.tag_update_new);
                }

                holder.mNavSubTitle.setText(item.getSub_title());
            }else{
                holder.mNavSubTitle.setVisibility(View.GONE);
            }

            if(item.isShowCacheSize()){
                holder.mCacheSize.setVisibility(View.VISIBLE);
                holder.mCacheSize.setText(item.getCacheSize());
            }else{
                holder.mCacheSize.setVisibility(View.GONE);
            }

        }
        return convertView;
    }

    static class ViewHolder{

        @Bind(R.id.nav_icon)
        ImageView mNavIcon;
        @Bind(R.id.nav_title)
        TextView mNavTitle;
        @Bind(R.id.nav_sub_title)
        TextView mNavSubTitle;
        @Bind(R.id.cache_size)
        TextView mCacheSize;
        @Bind(R.id.toggle)
        ToggleButton mToggleButton;
        View itemView;
        public ViewHolder(View itemView){
            this.itemView=itemView;
            ButterKnife.bind(this,itemView);
        }
    }
}
